{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thumbnail is 100-by-100\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "import requests\n",
    "# If you are using a Jupyter notebook, uncomment the following lines.\n",
    "%matplotlib inline\n",
    "# import matplotlib.pyplot as plt\n",
    "from PIL import Image\n",
    "from io import BytesIO\n",
    "\n",
    "endpoint = \"https://wgj1119.cognitiveservices.azure.com/\"\n",
    "subscription_key = \"6fdd2ccca89448e7ae1c29852f6553b2\"\n",
    "\n",
    "thumbnail_url = endpoint + \"vision/v3.1/generateThumbnail\"\n",
    "\n",
    "# Set image_url to the URL of an image that you want to analyze.\n",
    "image_url = \" http://huangnanhui.gitee.io/api-job/image/suoluetu.jpg\"\n",
    "\n",
    "# Construct URL\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "params = {'width': '100', 'height': '100', 'smartCropping': 'true'}\n",
    "data = {'url': image_url}\n",
    "# Call API\n",
    "response = requests.post(thumbnail_url, headers=headers, params=params, json=data)\n",
    "response.raise_for_status()\n",
    "\n",
    "# Open the image from bytes\n",
    "thumbnail = Image.open(BytesIO(response.content))\n",
    "\n",
    "# Verify the thumbnail size.\n",
    "print(\"Thumbnail is {0}-by-{1}\".format(*thumbnail.size))\n",
    "\n",
    "# Save thumbnail to file\n",
    "thumbnail.save('thumbnail.png')\n",
    "\n",
    "# Display image\n",
    "thumbnail.show()\n",
    "\n",
    "# Optional. Display the thumbnail from Jupyter.\n",
    "# plt.imshow(thumbnail)\n",
    "# plt.axis(\"off\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
